package com.rc.saas.tenant.service.operator;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.common.utils.StringUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.mapper.operator.TenantOperatorStationMapper;
import com.rc.saas.tenant.model.elec.ElecStation;
import com.rc.saas.tenant.model.elec.ElecStationExample;
import com.rc.saas.tenant.model.operator.TenantOperatorOpenService;
import com.rc.saas.tenant.model.operator.TenantOperatorOpenServiceExample;
import com.rc.saas.tenant.model.operator.TenantOperatorStation;
import com.rc.saas.tenant.model.operator.TenantOperatorStationExample;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.service.elec.ElecStationService;
import com.rc.saas.tenant.vo.elec.ElecStationResult;
import com.rc.saas.tenant.vo.elec.TenantOperatorStationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author frog
 * @date 2021/6/23
 */
@Service
public class TenantOperatorStationServiceImpl implements TenantOperatorStationService {

    @Autowired
    private TenantOperatorStationMapper tenantOperatorStationMapper;

    @Autowired
    @Lazy
    private ElecStationService elecStationService;

    @Autowired
    private TenantOperatorOpenServiceService tenantOperatorOpenServiceService;


    @Override
    public TenantOperatorStationMapper getMapper() {
        return tenantOperatorStationMapper;
    }

    @Override
    public PageInfo<TenantOperatorStationVo> pageTenantOperatorStation(BaseSearch baseSearch, Long elecStationId) {
        PageHelper.startPage(baseSearch.getOffset(), baseSearch.getLimit());
        List<TenantOperatorStationVo> tenantOperatorStationVOList = tenantOperatorStationMapper.pageTenantOperatorStation(elecStationId);
        return new PageInfo<TenantOperatorStationVo>(tenantOperatorStationVOList);
    }

    @Override
    public List<Long> listElecStationIdByTenantOperator(String tenantCode, String tenantOperatorCode) {
        List<Long> elecStationIdList = tenantOperatorStationMapper.listElecStationIdByTenantOperator(tenantCode, tenantOperatorCode);
        return elecStationIdList;
    }

    @Override
    public List<TenantOperatorStationVo> listOperatorByStationId(Long elecStationId) {
        return tenantOperatorStationMapper.listOperatorByStationId(elecStationId);
    }

    @Override
    public TResponse saveAccountSetting(String tenantType, String tenantOperatorCode, TenantUser user, ElecStation elecStation) {
        String proxyInvoice = "PROXY_INVOICE";

        String deleteMark = "0";

        //tenantType  0 : 本运营商   1:分销商
        List<TenantOperatorOpenService> tenantOperatorOpenServices = new ArrayList<>();

        if (StringUtil.isNotBlank(elecStation.getInvoiceProxyTenantCode())) {
            TenantOperatorOpenServiceExample example = new TenantOperatorOpenServiceExample();
            example.createCriteria().andTenantOperatorCodeEqualTo(elecStation.getInvoiceProxyTenantCode()).
                    andServiceTypeCodeEqualTo(proxyInvoice);
            tenantOperatorOpenServices = tenantOperatorOpenServiceService.selectByExample(example);

            //判断是否存在其他桩群
            if ("1".equals(elecStation.getProxyType())) {
                ElecStationExample stationExample = new ElecStationExample();
                stationExample.createCriteria().andInvoiceProxyTenantCodeEqualTo(elecStation.getInvoiceProxyTenantCode());
                List<ElecStation> elecStations = elecStationService.selectByExample(stationExample);

                if (elecStations.size() == 1) {
                    if (StringUtil.isNotBlank(elecStation.getInvoiceProxyTenantCode())) {
                        if (!CollectionUtils.isEmpty(tenantOperatorOpenServices)) {
                            deleteMark = "1";
                            tenantOperatorOpenServiceService.deleteByPrimaryKey(tenantOperatorOpenServices.get(0).getOpenServiceId());
                        }
                    }
                }
            }
        }

        if ("0".equals(tenantType)) {
            elecStation.setInvoiceProxyTenantCode(user.getTenantCode());
        } else if ("1".equals(tenantType)) {
            if (StringUtil.isNotBlank(elecStation.getInvoiceProxyTenantCode())) {
                TenantOperatorOpenServiceExample example = new TenantOperatorOpenServiceExample();
                example.createCriteria().andTenantOperatorCodeEqualTo(elecStation.getInvoiceProxyTenantCode()).
                        andServiceTypeCodeEqualTo(proxyInvoice);
                tenantOperatorOpenServices = tenantOperatorOpenServiceService.selectByExample(example);
            }

            if (CollectionUtils.isEmpty(tenantOperatorOpenServices) || "1".equals(deleteMark)) {
                TenantOperatorOpenService tenantOperatorOpenService = new TenantOperatorOpenService();
                tenantOperatorOpenService.setTenantCode(user.getTenantCode());
                tenantOperatorOpenService.setTenantOperatorCode(tenantOperatorCode);
                tenantOperatorOpenService.setServiceTypeCode(proxyInvoice);
                tenantOperatorOpenServiceService.insertSelective(tenantOperatorOpenService);
            }
            elecStation.setInvoiceProxyTenantCode(tenantOperatorCode);
        }
        elecStationService.updateByPrimaryKeySelective(elecStation);
        return TResponse.SUCCESS();
    }

    @Override
    public List<ElecStationResult> listElecStationByTenantOperator(String tenantOperatorCode) {
        return tenantOperatorStationMapper.listElecStationByTenantOperator(tenantOperatorCode);
    }

    @Override
    public TenantOperatorStation selectOperatorStationId(Long elecStationId, String tenantOperatorCode) {
        TenantOperatorStationExample tenantOperatorStationExample = new TenantOperatorStationExample();
        tenantOperatorStationExample.createCriteria().andElecStationIdEqualTo(elecStationId).andTenantOperatorCodeEqualTo(tenantOperatorCode);
        List<TenantOperatorStation> tenantOperatorStations = tenantOperatorStationMapper.selectByExample(tenantOperatorStationExample);
        if (CollectionUtils.isEmpty(tenantOperatorStations)) {
            return null;
        } else {
            return tenantOperatorStations.get(0);
        }
    }

    @Override
    public int countByOperatorStation(String tenantCode) {
        return tenantOperatorStationMapper.countByOperatorStation(tenantCode);
    }
}
